<?php
/**
 * 测评题目
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\weixin\controllers;

use app\libs\Method;
use app\libs\WeixinControl;
use app\modules\weixin\models\LowProduct;
use app\modules\weixin\models\LowProductClass;
use app\modules\weixin\models\LowProductClassRegistration;
use app\modules\weixin\models\LowProductClassYuyue;
use app\modules\weixin\models\MemberOpenid;
use yii;


class OrderController extends WeixinControl
{

    public $enableCsrfValidation = false;

    public function actionBuy(){
        $productId = Yii::$app->request->post('productId',1);
        $section = Yii::$app->request->post('section','阅读,写作');
        $price = Yii::$app->request->post('price',399);
        $orderNumber = 'sy'.time();
        if(!$this->uid){
            die(Method::jsonGenerate(99));
        }
        $model = new LowProductClassRegistration();
        $model->uid = $this->uid;
        $model->createTime = time();
        $model->save();
        $id = $model->primaryKey;
        $model = new LowProductClassRegistration();
        $model->uid = $this->uid;
        $model->pid = $id;
        $model->productId = $productId;
        $model->section = $section;
        $model->orderNumber = $orderNumber;
        $model->price = $price;
        $model->createTime = time();
        $model->save();
        $id = $model->primaryKey;
        die(Method::jsonGenerate(1,['id' => $id,'orderNumber' => $orderNumber,'price' => $price],'下单成功'));
    }

    public function actionPay(){
        $id = Yii::$app->request->get('id');
        $sign = LowProductClassRegistration::findOne($id);
        $TimeStart = date("YmdHis");
        $TimeExpire = date("YmdHis",time()+86400);
        $data = [
            "SvcTp" => "unified.trade.native",
            "MrchNo" => "260560013603",
            "MrchOrdNo" => $sign->orderNumber,
            "PurchDesc" => '399产品付款',
            "TotFee" => $sign->price*100,
            "TimeStart" => $TimeStart,
            "TimeExpire" => $TimeExpire,
            "TrmnlIP" => "8.140.25.250",
            "NotifyURL" => "https://www.thinkuprep.com/weixin/order/pay-return",
            "AcptTrmlInf" => [
                "TerminalType" => "11",
                "serialNum" => "op_device_id",
                "AppVersion" =>"1.000000"
            ]
        ];
        $re = Method::curl_post_jason("http://8.140.25.250:8888/baseapi/ItgMall/iabpQRCdItrScan",json_encode($data));
        $re = json_decode($re,true);
        return $this->redirect($re['Data']['QRCodeURL']);
    }

    /**
     * 回调
     */
    public function actionPayReturn(){
//        $file = "log.txt";
        $content = file_get_contents("php://input");
        $xml = simplexml_load_string($content,'SimpleXMLElement',LIBXML_NOCDATA);
        $json = json_encode($xml);
        $array = json_decode($json, true);
        $orderNumber = $array['out_trade_no'];
        $order = LowProductClassRegistration::find()->where("orderNumber='$orderNumber'")->one();
        if($order){
            $pay_result = $array['pay_result'];
            if($pay_result == 0){
                $order->status = 1;
                $order->save();
                $sign = LowProductClassRegistration::find()->where("pid=$order->pid AND status=0")->one();
                if(!$sign){
                    LowProductClassRegistration::updateAll(['status' => 1],"id=$order->pid");
                }
            }
        }
        $product = LowProduct::findOne($order->productId);
        $openInfo = MemberOpenid::find()->where("uid=$order->uid AND type=1")->one();
        $access_token = Method::token();
        $touser = $openInfo->openId; // 用户的 openid
        $template_id = 'ikw8oJ6xYErp_27a1VdngpWc6yTnz3CLu4kWxcheOcc'; // 模板 ID
        $data = array(
            'character_string2' => array(
                'value' => $order->orderNumber,
                'color' => '#173177'
            ),
            'thing3' => array(
                'value' => $product->name,
                'color' => '#173177'
            ),
            'amount9' => array(
                'value' => $order->price,
                'color' => '#173177'
            ),
            'time6' => array(
                'value' => date("Y-m-d H:i:s"),
                'color' => '#173177'
            ),
        );
        $url = 'https://wx.thinkwithu.com'; // 点击消息跳转的链接
        Method::sendTemplateMessage($access_token, $touser, $template_id, $data, $url);
        echo 'success';
    }
}